package com.fm.common.net.baseCoroutine

import com.ticket.rccl.net.baseCoroutine.listener.CoroutineErrorListener
import kotlinx.coroutines.*

/**
 * @CreateData:     2020/12/22
 * @Author:         lyy
 * @Description:    协程 处理封装
 */

val UI: CoroutineDispatcher = Dispatchers.Main

val IO: CoroutineDispatcher = Dispatchers.IO

val Default: CoroutineDispatcher = Dispatchers.Default

// 运行在主线程，支持异常处理、无返回结果
fun runOnUI(block: suspend CoroutineScope.() -> Unit) = uiScope().launch(block = block)

// 运行在后台线程，支持异常处理、无返回结果
fun runInBackground(block: suspend CoroutineScope.() -> Unit) = ioScope().launch(block = block)

// 运行在主线程，支持异常处理、有返回结果
fun <T> asyncOnUI(block: suspend CoroutineScope.() -> T) = uiScope().async(block = block)

// 运行在后台线程，支持异常处理、有返回结果
fun <T> asyncInBackground(block: suspend CoroutineScope.() -> T) = ioScope().async(block = block)


fun ioScope(errorHandler: CoroutineErrorListener? = null) = SafeCoroutineScope(IO, errorHandler)

fun uiScope(errorHandler: CoroutineErrorListener? = null) = SafeCoroutineScope(UI, errorHandler)

fun defaultScope(errorHandler: CoroutineErrorListener? = null) = SafeCoroutineScope(Default, errorHandler)

fun customScope(dispatcher: CoroutineDispatcher, errorHandler: CoroutineErrorListener? = null) = SafeCoroutineScope(dispatcher, errorHandler)